<?php

namespace app\common\model;

use think\Model;

class BaseModel extends Model
{
    // 自动更新时间
    public $timestamps = true;

    // 定义时间戳字段名
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    /**
     * 查询单条数据
     * @param $where
     * @param bool $field
     * @param string $order
     * @return false
     */
    public function getFindData($where, $field = true, $order = ['id' => 'desc'])
    {
        if (empty($where) && ! is_array($where)) {
            return false;
        }
        $res = $this->where($where)->field($field);
        if ($order) {
            $res->order($order);
        }
        return $res->find();
    }

    /**
     * 查询多条数据
     * @param $where
     * @param bool $field
     * @param int $page
     * @param int $num
     * @param string[] $order
     * @return mixed
     */
    public function getDataByWhere($where, $field = true, $page = 0, $num = 10, $order = ['id' => 'desc'])
    {
        $res = $this->field($field);
        if (! empty($where) && is_array($where)) {
            $res->where($where);
        }
        if ($page > 0) {
            $res->page($page, $num);
        }
        return $res->order($order)->select();
    }
}
